package com.adcc.foc.controller;

import com.adcc.foc.domain.ResultState;
import com.adcc.foc.domain.TreeNode;
import com.adcc.foc.domain.User;
import com.adcc.foc.service.OrganizationService;
import com.adcc.foc.service.UserService;
import com.adcc.foc.utils.Log;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by houxy on 2016/8/3.
 *
 * @author houxy
 * 组织维护树形节点
 */
@Controller
public class MaintainTreeNode {

    @Resource
    private UserService userService;

    @Resource
    private OrganizationService organizationService;

    /**
     * 转到组织节点维护页面
     *
     * @author houxy
     */
    @RequestMapping("toTreeNode")
    public String totreenode() {
        return "treenode";
    }

    /**
     * 获取所有组织节点
     *
     * @author houxy
     */
    @RequestMapping("/getNodeList")
    @ResponseBody
    public List<TreeNode> getFatherNode(HttpServletRequest req) {
        List<TreeNode> list = new ArrayList<TreeNode>();
        try {
            String isShowAll = (String) req.getSession().getAttribute("isShowAll");
            String userName = (String) req.getSession().getAttribute("userName");
            if ("-1".equals(isShowAll)) {
                throw new RuntimeException("你无此权限!");
            }
            User user = userService.findUserByUserName(userName);
            list = organizationService.getNodeList(user.getOrgid(), isShowAll);
        } catch (Exception e) {
            Log.error(ExceptionUtils.getFullStackTrace(e), MaintainTreeNode.class);
        }
        return list;
    }

    /**
     * 修改节点名称
     *
     * @param id       节点id
     * @param deptName 修改的新名
     * @author houxy
     */
    @RequestMapping("/alterName")
    @ResponseBody
    public Map<String, Object> alterName(String id, String deptName) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            map = organizationService.alterName(id, deptName);
            map.put("status", ResultState.SUCCESS);
        } catch (Exception e) {
            Log.error(ExceptionUtils.getFullStackTrace(e), MaintainTreeNode.class);
            map.clear();
            map.put("status", ResultState.ERROR);
        }
        return map;
    }

    /**
     * 创建一个新节点
     *
     * @param pid      父节点id
     * @param deptName 创建节点的名字
     * @return
     */
    @RequestMapping("/newNode")
    @ResponseBody
    public Map<String, Object> newNode(String pid, String deptName) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            map = organizationService.newNode(pid, deptName);
            map.put("status", ResultState.SUCCESS);
        } catch (Exception e) {
            Log.error(ExceptionUtils.getFullStackTrace(e), MaintainTreeNode.class);
            map.clear();
            map.put("status", ResultState.ERROR);
        }
        return map;
    }

    /**
     * 删除选中节点以及其子节点
     *
     * @param nodeId 选中节点的id
     * @author houxy
     */
    @RequestMapping("/deleteNode")
    @ResponseBody
    public Map<String, Object> deleteNode(String nodeId) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            map = organizationService.deleteNode(nodeId);
            map.put("status", ResultState.SUCCESS);
        } catch (Exception e) {
            Log.error(ExceptionUtils.getFullStackTrace(e), MaintainTreeNode.class);
            map.clear();
            map.put("status", ResultState.ERROR);
        }
        return map;
    }

    /**
     * 转移选中节点
     *
     * @param sid 选中节点的id
     * @param pid 想要转移到某个节点下的id
     * @author houxy
     */
    @RequestMapping("/moveNode")
    @ResponseBody
    public Map<String, Object> moveNode(String sid, String pid) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            map = organizationService.moveNode(sid, pid);
            map.put("status", ResultState.SUCCESS);
        } catch (Exception e) {
            Log.error(ExceptionUtils.getFullStackTrace(e), MaintainTreeNode.class);
            map.clear();
            map.put("status", ResultState.ERROR);
        }
        return map;
    }

}
